import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/entity/Restful.dart';
import 'package:mahjong/entity/UserEntityInfo.dart';
import 'package:mahjong/entity/meta/MetaRoomEntity.dart';
import 'package:mahjong/page/mahjong/MahjongGameIndex.dart';
import 'package:mahjong/util/CustomerDialog.dart';
import 'package:mahjong/util/DictionaryConst.dart';
import 'package:mahjong/util/Http.dart';
import 'package:mahjong/const/RoomType.dart';
import 'package:mahjong/util/ProviderClean.dart';
import 'package:mahjong/widget/CommonAlterDialog.dart';
import 'package:provider/provider.dart';
import 'package:mahjong/util/InsertHome.dart';
import '../croom/WzMahjong.dart';
import '../croom/GoldenFlower.dart';
import '../croom/ChengduMahjong.dart';

class CreateHomeDialog extends StatefulWidget {
  final String title, description;
  final Image image;

  CreateHomeDialog({
    @required this.title,
    @required this.description,
    this.image,
  });
  @override
  _CreateHomeDialogState createState() => _CreateHomeDialogState();
}

class CrRoomType {
  String title;
  bool choice;
  Widget crWidget;
  String roomType;
  CrRoomType.init({this.title, this.choice, this.crWidget, this.roomType});
}

class _CreateHomeDialogState extends State<CreateHomeDialog> {
  MetaRoomEntity _metaRoomEntity = new MetaRoomEntity();

  List<CrRoomType> _rooms;

  Widget _crRoom;

  int _selectIndex;

  @override
  void initState() {
    super.initState();
    _rooms = [
      CrRoomType.init(
          title: "万州麻将",
          crWidget: WzMahjongCroom(_metaRoomEntity),
          roomType: RoomType.WZ_MAHJONG),
      CrRoomType.init(
          title: "成都麻将", crWidget: ChengduMahjong(null), roomType: null),
      CrRoomType.init(
          title: "传说中", crWidget: GoldenFlower(null), roomType: null)
    ];
    _crRoom = _rooms[0].crWidget;
    _metaRoomEntity.roomType = _rooms[0].roomType;
    _selectIndex = 0;
  }

  @override
  Widget build(BuildContext context) {
    return CommonAlterDialog(
      backgroundColor: Colors.white,
      child: Row(
        children: [
          Expanded(
              flex: 2,
              child: Container(
                color: Colors.green[500],
                child: ListView.builder(
                  itemCount: _rooms.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                        onTap: () {
                          _crRoom = _rooms[index].crWidget;
                          _selectIndex = index;
                          _metaRoomEntity.roomRule = "";
                          setState(() {});
                        },
                        child: Container(
                          alignment: Alignment.center,
                          height: 50.h,
                          decoration: BoxDecoration(
                              color: index == _selectIndex
                                  ? Colors.grey[300]
                                  : Colors.green[500],
                              border: Border(
                                  bottom: BorderSide(color: Colors.white))),
                          child: Text(
                            _rooms[index].title,
                            style: TextStyle(
                                color: index == _selectIndex
                                    ? Colors.green[500]
                                    : Colors.grey[300],
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold),
                          ),
                        ));
                  },
                ),
              )),
          Expanded(
              flex: 9,
              child: Container(
                color: Colors.grey[300],
                padding: EdgeInsets.all(10.h),
                child: Column(
                  children: [
                    Expanded(
                        child: Container(
                      child: SingleChildScrollView(
                        child: _crRoom,
                      ),
                    )),
                    Container(
                      color: Colors.grey[300],
                      height: 30.h,
                      alignment: Alignment.center,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          ElevatedButton(
                              style: ButtonStyle(
                                shape:
                                    MaterialStateProperty.all(StadiumBorder()),
                                backgroundColor:
                                    MaterialStateProperty.resolveWith((states) {
                                  //设置按下时的背景颜色
                                  if (states.contains(MaterialState.pressed)) {
                                    return Colors.blue[200];
                                  }
                                  //默认不使用背景颜色
                                  return Colors.green[500];
                                }),
                                elevation: MaterialStateProperty.all(0),
                              ),
                              onPressed: () {
                                if (_metaRoomEntity.roomRule != "" &&
                                    _metaRoomEntity.roomType != null) {
                                  Http.getInstance().request<Restful>(
                                      DictionaryConst.createRoom,
                                      type: RequestEnum.POST,
                                      contentType: ContentType.text,
                                      responseType: ResponseType.json,
                                      showLoading: true,
                                      loadingMsg: "连接服务器中...",
                                      data: {
                                        "roomType":
                                            _rooms[_selectIndex].roomType,
                                        "roomRule": _metaRoomEntity.roomRule
                                      }, success: (data) {
                                    MetaRoomEntity roomEntity =
                                        MetaRoomEntity.fromJson(data.body);
                                    InsertHome.addHome(
                                        context,
                                        _rooms[_selectIndex].roomType,
                                        roomEntity.code);
                                  }, error: (data) {});
                                } else {
                                  CustomerDialog.showText("无法创建房间");
                                }
                              },
                              child: Text("创建"))
                        ],
                      ),
                    )
                  ],
                ),
              ))
        ],
      ),
    );
  }
}
